/**
 * @description: compose
 * @author: huen2015
 */
import { IFn } from './types'

function compose(...args: IFn[]): IFn {
	if (args.length === 0) return () => {}

	if (args.length === 1) {
		return (...otherArgs: any[]) => args[0](...otherArgs)
	}

	return args.reduce(
		(pre: IFn, cur: IFn) =>
			(...otherArgs) =>
				pre(cur(...otherArgs)),
	)
}

const fn1 = (p1: string) => {
	console.log('p1', p1)
	return 'fn1' + p1
}

const fn2 = (p2: string) => {
	console.log('p2', p2)
	return 'fn2' + p2
}

const fn3 = (p3: string) => {
	console.log('p3', p3)
	return 'fn3' + p3
}

fn1(fn2(fn3('test')))
console.log('compose')
compose(fn1, fn2, fn3)('test')
